package demo.vo.http;

import org.springframework.http.HttpStatus;

import com.alibaba.fastjson.JSONObject;

/**
 * http response包装类
 * 
 */
public class Response {

    /** http状态 */
    private int responseCode;

    /** 返回内容的MIME类型 ,如：Content-Type: text/plain;charset=UTF-8 */
    private String contentType;

    /** 消息长度 ，如：Content-Length: 62 */
    private int contentLength;

    /**
     * Date头域表示消息发送的时间，缓存在评估响应的新鲜度时要用到， 客户端有一种方法能判断响应是否来自缓存，就是使用Date首部。
     * 将响应中Date首部的值与当前时间进行比较，如果响应中的日期值比较早，客户端通常就可以认为是来自缓存的；
     */
    private long date;

    /** 返回的数据 */
    private String content;

    /**
     * @param responseCode
     * @param contentType
     * @param contentLength
     * @param date
     * @param content
     */
    public Response(int responseCode, String contentType, int contentLength, long date, String content) {
        super();
        this.responseCode = responseCode;
        this.contentType = contentType;
        this.contentLength = contentLength;
        this.date = date;
        this.content = content;
    }

    public int getResponseCode() {
        return responseCode;
    }

    public String getContentType() {
        return contentType;
    }

    public long getDate() {
        return date;
    }

    public int getContentLength() {
        return contentLength;
    }

    public boolean isHttpStatus2XX() {
        return responseCode >= 200 && responseCode < 300;
    }

    public HttpStatus getHttpStatus() {
        return HttpStatus.valueOf(responseCode);
    }

    public String getContent() {
        return content;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
